numpy indexing
Official documents
https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
hr.icon
Tips
Numpy配列のAdvanced Indexingは知らないとわけわからなくなる。([], compare, condition, 条件式, fancy indexing ) - Qiita
https://qiita.com/enoughspacefor/items/cc5a9eb490d1a1afd1cb
MetPy Mondays '#90 - What is a NumPy Axis? : Unidata Developer's Blog
https://www.unidata.ucar.edu/blogs/developer/entry/metpy-mondays-90-what-is
hr.icon
Examples
When f contains frequencies and P contains spectral density, the top 10 corresponding frequencies in descending order of 𝑃
code:python
fnp.argsort(P)-10:
range selection 1
code:python
import numpy as np
# lon: longitude lat:latittude
xrange=np.logical_and(lon>=120.,lon<=180.)
yrange=np.logical_and(lat>=20.,lat<=45.)
sst=(sstyrange,:):,xrange
range selection 2
code:python
import numpy as np
# lon: longitude lat:latittude
xrange=((lon>136.) & (lon<140.))
yrange=((lat>36) & (lat<38))
lonxrange
latyrange
sst=(sst,xrange)yrange,:
range selection 3
code:python
import numpy as np
# lon: longitude lat:latittude
x=np.where(np.logical_and(lon>=120.,lon<=180.))0
y=np.where(np.logical_and(lat>=20.,lat<=30.))0
sst=temp[y0:y-1+1,x0:x-1+1]
find nearest value in numpy array
http://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array
code:python
import numpy as np
def find_nearest(array,value):
idx=(np.abs(array-value)).argmin()
return arrayidx
find index where 2-dimensioal (multi-dimensional) array has maximum or minimum
code:python
ans=np.where(a == a.max())
i=ans00
j=ans10
i,j = np.unravel_index(a.argmax(), a.shape)
MetPy Mondays #48 - Satellite Data with CLASS Part 5 : Unidata Developer's Blog
MetPy Mondays #320 - Extracting Time Series Data from NDFD GRIB Files : Unidata Developer's Blog
The index at which the array 'walk' first exceeds 10.
code:python
(np.abswalk>=10).argmax()
NOT ~
code:python
a=np.arange(5)
a~(a>2)
array(0, 1, 2)
Tips
python - Find where a NumPy array is equal to any value in a list of values - Stack Overflow
...
MetPy Mondays
MetPy Mondays #304 - Dot... Dot... Dot... : Unidata Developer's Blog
code:python
# a:,... instead of a0,:,:
hr.icon
Useful methods
argmax()
argmin()
searchsorted()
argsort()
argwhere()